import { ref } from "vue"
import { useCascaderAreaData } from "@vant/area-data"
export const isExternal = (path: string) => {
  const reg = /^(https?:|mailto:|tel:)/
  return reg.test(path)
}

export const isArray = (arg: any) => {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]"
  }
  return Array.isArray(arg)
}

export function isString(str: any) {
  if (typeof str === "string" || str instanceof String) {
    return true
  }
  return false
}

export const isValidURL = (url: string) => {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

export const checkPhoneNumber = (rule: any, value: any, callback: any) => {
  const reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
  if (!value) {
    return callback(new Error("请输入手机号！"))
  }
  setTimeout(() => {
    if (!reg.test(value)) {
      callback(new Error("手机号格式错误！"))
    } else {
      callback()
    }
  }, 100)
}
// 身份证号
export const checkID = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("请输入身份证号！"))
  }
  setTimeout(() => {
    const arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] //加权因子
    const arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2] //校验码
    if (/^\d{17}\d|x$/i.test(value)) {
      let sum = 0
      const idx = ref<any>()
      for (let i = 0; i < value.length - 1; i++) {
        // 对前17位数字与权值乘积求和
        sum += parseInt(value.substr(i, 1), 10) * arrExp[i]
      }
      // 计算模（固定算法）
      idx.value = sum % 11
      // 检验第18为是否与校验码相等
      if (arrValid[idx.value] == value.substr(17, 1).toUpperCase()) {
        callback()
      } else {
        callback(new Error("身份证号不正确！"))
      }
    } else {
      callback(new Error("身份证号不正确！"))
    }
  }, 100)
}
// 中文名字验证
export const checkName = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("请输入姓名！"))
  }
  setTimeout(() => {
    if (/^(?:[\u4e00-\u9fa5·]{2,16})$/.test(value)) {
      callback()
    } else {
      callback(new Error("请输入中文名字"))
    }
  }, 100)
}
export const checkNumber = (rule: any, value: any, callback: any) => {
  const reg = /^\+?[1-9]\d*$/
  if (!value) {
    return callback(new Error("请输入金额！"))
  }
  setTimeout(() => {
    if (!reg.test(value)) {
      callback(new Error("只能为正整数！"))
    } else {
      callback()
    }
  }, 100)
}
export const isAddressDetailed = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("请输入地址！"))
  }
  setTimeout(() => {
    const provinces = useCascaderAreaData()
    const pArr = provinces.map((item: any) => {
      return item.text
    })

    const inputString = value.trim().split(" ")
    // console.log(`${inputString[0]}${inputString[1]}`)
    if (inputString.length != 4 || !pArr.includes(inputString[0]) || inputString.includes("")) {
      callback(new Error("输入地址无效或者不符合规范！"))
    } else {
      callback()
    }
  }, 100)
}
export const checkPw = (rule: any, value: any, callback: any) => {
  const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
  if (!value) {
    return callback(new Error("请输入密码！"))
  }
  setTimeout(() => {
    if (!reg.test(value) && value.length <= 6) {
      callback(new Error("请设置数字加字母组合且长度不能少于6位数的密码！"))
    } else {
      callback()
    }
  }, 100)
}
export const checkInvite = (rule: any, value: any, callback: any) => {
  const reg = /^[A-Z0-9]{6}$/
  setTimeout(() => {
    if (!reg.test(value) && value !== "") {
      callback(new Error("邀请码不符合要求！"))
    } else {
      callback()
    }
  }, 100)
}
